package com.vv.geo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.beem.project.beem.R;
import com.vv.geo.poller.LocationPoller;
import com.vv.geo.poller.LocationPollerParameter;
//import com.google.android.maps.MapActivity;
//import com.google.android.maps.MapController;
//import com.google.android.maps.MapView;
//import com.google.android.maps.MyLocationOverlay;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Main extends Activity implements OnCheckedChangeListener {

	private ToggleButton tb;
	private Intent i;
	private SharedPreferences login;
	private SharedPreferences.Editor edit;
	private static final String C2DM_UNREG = "com.google.android.c2dm.intent.UNREGISTER";
	private static final int PERIOD=2000; 	// 30 sec
	private PendingIntent pi=null;
	private AlarmManager mgr=null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loadmain);
        login=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        edit=login.edit();
        edit.putString("loginstatus", "online");
		edit.commit();
        tb=(ToggleButton)findViewById(R.id.maintooglebutton);
        try {
            Class.forName("android.os.AsyncTask");
        } 
        catch (ClassNotFoundException e) {
        }
        i=new Intent(Main.this, LocationSynch.class);
        i.putExtra("username", login.getString("username", ""));
        tb.setOnCheckedChangeListener(this);
        
        /*fragment which start location polling service in background*/
        
        mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    	Intent i=new Intent(this, LocationPoller.class);
    	Bundle bundle = new Bundle();
    	LocationPollerParameter parameter = new LocationPollerParameter(bundle);
    	parameter.setIntentToBroadcastOnCompletion(new Intent(this, LocationReceiver.class));
    	// First try for GPS and fall back to NETWORK_PROVIDER
    	parameter.setProviders(new String[] {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER});
    	parameter.setTimeout(6000);
    	i.putExtras(bundle);
   		pi=PendingIntent.getBroadcast(this, 0, i, 0);
   		Log.d("ghfghfg ","----"+login.getString("loginstatus",""));
   		tb.setChecked(true);
   		if(login.getString("loginstatus","").equals("online")){
        	tb.setChecked(true);
   		}
        else if(login.getString("loginstatus","").equals("offline")){
        	tb.setChecked(false);
        }
	}//on create ends here
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
//------------------------------------------------option menu code------------------------------------------------
	
//----------------------------------*option menu end here*-----------------------------------------------------------	
//	@Override
//	protected boolean isRouteDisplayed() {
//		// TODO Auto-generated method stub
//		return false;
//	}
//----------------------------------toggle button------------------------------------------------------------------
	@Override
	public void onCheckedChanged(CompoundButton b, boolean s) {
		try{
		// TODO Auto-generated method stub
		if(s==true)
		{
			if(pi!=null && mgr!=null)
			{
//				PostToggle online=new PostToggle();
//				online.execute("1");
				mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),PERIOD,pi);
				edit.putString("loginstatus", "online");
				edit.commit();
				Log.d("loginstatus",login.getString("loginstatus", ""));
				Log.d("service start","start");
			}
		}
		else
		{
//			PostToggle online=new PostToggle();
//			online.execute("0");
			mgr.cancel(pi);
			edit.putString("loginstatus", "offline");
			edit.commit();
			Log.d("loginstatus",login.getString("loginstatus", ""));
			Log.d("service stoped","stop");
			//send off line flag
		}
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}//end checked change
//-------------------------------------toggle button code ends here----------------------------------------------
	
	 
}


